#include "Speaker.h"

Speaker::Speaker(){
	pVoice =NULL;


	speaking=false;

	//Initialization
	if(FAILED(::CoInitialize(NULL))){
		std::cout<<"Could not initialize the Speaker. I am so terribly sorry!"<<std::endl;
				return;
	}

	//Create voice
	    hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);

	if( FAILED( hr ) )
    {
		std::cout<<"Could not CoCreate Instance for the Speaker."<<std::endl;
				return;

    }

}


Speaker::~Speaker(){
        pVoice->Release();
        pVoice = NULL;
	//Unitnitialize
	::CoUninitialize();
}

void Speaker::Say(const wchar_t * sentence){
	while(speaking)
		//Wait until previous speech is completed
		Sleep(500);
	if(!speaking){
		speaking=true;
		hr=pVoice->Speak(sentence, 0, NULL);
		speaking=false;
	}
}

bool Speaker::isSpeaking(void){
	return speaking;
}


VOID WINAPI Sleep(
DWORD dwMilliseconds
);
